package gui.mainFrame.codeViewer;

import java.util.Scanner;
import testGenerator.testMethodModel.AutoJUnitMethod;

/**
 * Determines the caret position of the given method and branch.
 * @author William Whitney
 */
public class SourceCaretFinder
{

    private final String code;
    private final AutoJUnitMethod method;

    public SourceCaretFinder(String code, AutoJUnitMethod method)
    {
        this.code = code;
        this.method = method;
    }

    public int getMethodStartCaretPosition()
    {
        int caretCount = 0;

        Scanner reader = new Scanner(code);

        while (reader.hasNextLine())
        {
            String methodStr = "";
            if (method.isConstructor())
            {
                methodStr = method.getAsStringSimple();
            }
            else
            {
                methodStr = method.getAsString();
            }
            //Remove all spaces
            methodStr = methodStr.replace(" ", "");

            String line = reader.nextLine();
            caretCount += line.length() + 1;
            //Remove all spaces
            line = line.replace(" ", "");

            //Look for method signature in line
            if (line.contains(methodStr))
            {
                //Remove new line
                caretCount -= 1;
                break;
            }
        }

        return caretCount;
    }

   
}
